# -*- coding:utf-8 -*-
from flask import Flask
from flask_script import Manager
from application.utils import load_config,load_command,load_blueprint,load_redis
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate,MigrateCommand
from flask_session import Session
from application.utils.log import init_log

# 创建应用对象
app = Flask(__name__)


# 创建数据库对象
db = SQLAlchemy()

# redis预设全局变量
redis_store = None


# 数据迁移
migrate = Migrate()

# 终端脚本管理对象初始化
manager = Manager()


def init_app(config_path):
    # 自动加载配置
    Config = load_config(config_path)
    app.config.from_object(Config)
    # 挂载应用对象
    manager.app = app
    # 自动加载终端命令
    load_command(manager, "application.utils.commands")

    # redis的链接初始化
    # global redis_store
    # redis_store = StrictRedis(host=Config.REDIS_HOST, port=Config.REDIS_PORT, db=Config.REDIS_DB)
    global redis_store
    redis_store = load_redis(app)

    # # 开启session功能
    # Session(app)

    # 数据库初始化
    db.init_app(app)

    # 启动日志
    init_log(Config)

    # 注册蓝图对象
    load_blueprint(app,db)

    # 数据迁移初始化
    migrate.init_app(app,db)
    manager.add_command("db", MigrateCommand)

    # 对外暴露终端管理对象
    return manager, app


